package com.zhdl.modules.assemblybolt;

import com.project.common.util.DelayUtil;
import com.zhdl.common.eventbus.center.EventBusCenter;
import com.zhdl.modules.assemblybolt.service.AssemblyBoltService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.scheduling.concurrent.ThreadPoolTaskScheduler;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

@Slf4j
@Service
public class KukaRobotProcess {

    /**
     * 当前流程是否在运行 - 默认未运行
     * 当前流程是否在测试 - 默认未测试
     * true 运行 false 未运行
     */
    public boolean isRunning = false, isTest = false;
    @Resource
    public ThreadPoolTaskScheduler taskScheduler1;
    @Resource
    public AssemblyBoltService assemblyBoltService;
    int robotProcess = -1;

    public void release() {
        robotProcess = -1;
        isRunning = false;
    }

    /**
     * 初始化
     */
    public void init() {
        EventBusCenter.getEventBus().register(this);
        release();
    }

    /**
     * 机器人流程开始
     */
    public void robotStartProcess() {
        /*
            判断当前流程是否正在运行
         */
        if (isRunning) {
            return;
        }
        isRunning = true;

        log.info("------------------流程开始运动------------------");

        robotProcess = 0;
        nextRobot();
    }

    public void nextRobot() {
        robotProcess++;
        switch (robotProcess) {
            case 1 -> {
                assemblyBoltService.kukaSendCmd(1, s -> {
                    nextRobot();
                    log.info("库卡机器人-到达拍照位置");
                });
            }
            case 2 -> {
                assemblyBoltService.kukaSendCmd(2, s -> {
                    nextRobot();
                    log.info("库卡机器人-获取抓料点，并运动到抓料点");
                });
            }
            case 3 -> {
                nextRobot();
            }
            case 4 -> {
                DelayUtil.delayedExecution(10000, () -> {
                    assemblyBoltService.kukaSendCmd(4, s -> {
                        log.info("库卡机器人-抓住托盘完成,开始运动到放料点");
                    });
                });
            }
            case 5 -> {
                assemblyBoltService.kukaSendCmd(5, s -> {
                    nextRobot();
                    log.info("库卡机器人-完成加工,开始抓住完成的托盘");
                });
            }
            case 6 -> {
                DelayUtil.delayedExecution(10000, () -> {
                    assemblyBoltService.kukaSendCmd(6, s -> {
                        nextRobot();
                        log.info("库卡机器人-抓取完成 抬起");
                    });
                });
            }
            case 7 -> {
                assemblyBoltService.kukaSendCmd(7, s -> {
                    nextRobot();
                    log.info("库卡机器人-开始运动到小车上方");
                });
            }
            case 8 -> {
                assemblyBoltService.kukaSendCmd(8, s -> {
                    nextRobot();
                    log.info("库卡机器人-开始放下");
                });
            }
            case 9 -> {
                assemblyBoltService.kukaSendCmd(9, s -> {
                    nextRobot();
                    release();
                    log.info("库卡机器人-放下完成 回去初始点");
                });
            }
        }
    }
}
